﻿using SpectralCamera.Extend;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SpectralCamera.Model
{
    public class CrameHelper
    {
        public static async void ConnectedCrame()
        {
            //CMDHelper.SetLocalIpCheck();

            // 服务端初始化
            //var server = new TcpHelper.TcpServer();
            //var localIP = TcpHelper.GetPreferredLocalIP();

            //server.ClientConnected += (s, e) =>
            //{
            //    Console.WriteLine($"New client: {e.Connection.RemoteEndPoint}");
            //};

            //server.DataReceived += (s, e) =>
            //{
            //    Console.WriteLine($"From {e.Connection.RemoteEndPoint}: {e.GetString()}");
            //    server.SendAsync(e.Connection, Encoding.UTF8.GetBytes("Received!"));
            //};

            //await server.StartAsync(localIP, 8080);
            IPAddress ipAddress = IPAddress.Parse("192.168.0.4");
            // 客户端连接
            var client = new TcpHelper.TcpClientHelper();
            client.DataReceived += (s, e) =>
                Console.WriteLine($"Server says: {e.GetString()}");

            //await client.ConnectAsync(localIP, 8080);
            await client.ConnectAsync(ipAddress, 700);
            await client.SendAsync(Encoding.UTF8.GetBytes("Hello Server"));

            // 保持运行
            Console.ReadLine();

            // 清理资源
            //server.Dispose();
            client.Dispose();
        }
    }
}
